Added basic doc for dealing with DTO#369
Conversation
| } | ||
| ``` | ||
|
|
||
| In this case, we disable all operation exept `POST`. |
dunglas
left a comment
There was a problem hiding this comment.
Can you also make classes final please?
|
|
||
| use ApiPlatform\Core\EventListener\EventPriorities; | ||
| use App\Manager\UserManager; | ||
| use Joli\Entity\User; |
|
|
||
| ## How to use a DTO for writing | ||
|
|
||
| Sometimes it's easier to use a DTO than an Entity when performing simple that. |
| @@ -0,0 +1,116 @@ | |||
| # DTO - Data Transfer Object | |||
There was a problem hiding this comment.
May I suggest: Handling Data Transfer Objects (DTOs)
| @@ -0,0 +1,116 @@ | |||
| # DTO - Data Transfer Object | |||
|
|
|||
| ## How to use a DTO for writing | |||
There was a problem hiding this comment.
We use title case for titles: How to use a DTO for Writing
| ## How to use a DTO for writing | ||
|
|
||
| Sometimes it's easier to use a DTO than an Entity when performing simple that. | ||
| For exemple, if the application should be able to send an email when someone has |
| } | ||
| ``` | ||
|
|
||
| In this case, we disable all operation exept `POST`. |
|
|
||
| In this case, we disable all operation exept `POST`. | ||
|
|
||
| Then, thanks to the [the event system](events.md), it's possible to intercept |
| public static function getSubscribedEvents() | ||
| { | ||
| return [ | ||
| KernelEvents::VIEW => [ |
There was a problem hiding this comment.
IIRC, you can remove the outer array.
There was a problem hiding this comment.
yes it's possible. But usually the listener can be re-used to handle many operation.
And it could avoid api-platform/core#1590 (comment)
Anyway, I removed the outer array
| ## How to use a DTO for writing | ||
|
|
||
| Sometimes it's easier to use a DTO than an Entity when performing simple that. | ||
| For exemple, if the application should be able to send an email when someone has |
There was a problem hiding this comment.
For example, the application should be able to send an email when someone has lost its password.
|
|
||
| ## How to use a DTO for writing | ||
|
|
||
| Sometimes it's easier to use a DTO than an Entity when performing simple that. |
| } | ||
| ``` | ||
|
|
||
| Then this class should be registred as a service, then tagged. |
|
@alanpoulain & @dunglas Thanks for the review. I have addressed your comments. |
|
Thanks @lyrixx! Great piece of docs. |
|
added one missing step in #371 |
| { | ||
| $request = $event->getRequest(); | ||
|
|
||
| if ('api_forgot_password_requests_post_collection' !== $request->attributes->get('_route')) { |
There was a problem hiding this comment.
wouldn't better to check the model instance instead ?
if (
!Request::METHOD_POST === $request->getMethod()
|| !$event->getControllerResult() instanceof ForgotPasswordRequest
) {
return;
}There was a problem hiding this comment.
Actually, Now I prefer to match the route name. This reduce side effect.
But anyway, It's not really perfect IMHO
There was a problem hiding this comment.
ahh ok, IMO there is no perfect way ;)
Fixed api-platform/core#1590